package cn.com.netedge.iot.frontend.comm.utils;

import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.mq.MqMessageListener;
import cn.com.netedge.iot.common.utils.BaseJmsMsgUtil;
import cn.com.netedge.iot.frontend.comm.constants.AppConstant;
import cn.com.netedge.iot.frontend.comm.mq.DownMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("feCommJmsMsgUtil")
public class JmsMsgUtil extends BaseJmsMsgUtil {
    private static final Logger logger = LoggerFactory.getLogger(JmsMsgUtil.class);

    @Override
    public void subscribeQueues() {
        List<String> queueList = new ArrayList<>(2);
        queueList.add(MqConstants.QUEUE_DOWN_MESSAGE);
        queueList.add(MqConstants.QUEUE_DOWN_MESSAGE + "." + AppConstant.machineName);
        subscribeQueue(queueList);
    }

    @Override
    public MqMessageListener getMessageListener(String queueName) {
        MqMessageListener listener;
        if (queueName.indexOf(MqConstants.QUEUE_DOWN_MESSAGE) >= 0) {
            listener = AppConstant.APP_CONTEXT.getBean(DownMessageListener.class);
        } else {
            logger.debug("queueName:{} not support subscribe");
            return null;
        }
        return  listener;
    }
}
